package vn.lumi.smarthome.network;

public abstract class NetMessage { 
	
	//cmd id
	public final static int CMD_NOTHING = 0x00;
	public final static int CMD_APP_LOGIN = 0xA0;
	public final static int CMD_HC_LOGIN_FEEDBACK = 0xA2;
	public final static int CMD_APP_CONTROL_NORMAL = 0xB0;
	public final static int CMD_APP_CONTROL_RGB = 0xB1;
	public final static int CMD_APP_END_CONTROL_RGB = 0xB2;
	public final static int CMD_APP_KEEP_ALIVE = 0xC0;
	public final static int CMD_HC_ACK = 0xF0;
	public final static int CMD_HC_BUSY= 0xF1; 
	public final static int CMD_APP_RECONNECT = 0xC1;
	public final static int CMD_HC_UPDATE_DEVICE_STATUS = 0xB3;
	public final static int CMD_HC_CONFIG = 0xA3;
	public final static int CMD_HC_UPDATE_ALL_DEVICE = 0xA4;
	public final static int CMD_APP_LOGOUT = 0xA1;
	public final static int CMD_APP_CHANGE_PASSWORD = 0xA5;
	public final static int CMD_NOTIFY_LOGOUT =  0xA6;
	public final static int CMD_HC_SORT_DEV =  0xA7;
	
	public final static int CMD_APP_CREATE_SCENE = 0xD0;
	public final static int CMD_APP_EDIT_SCENE = 0xD5; 
	public final static int CMD_HC_ACK_SCENE = 0xD2;
	public final static int CMD_APP_DEL_SCENE = 0xD3;
	public final static int CMD_APP_ACTIVATE_SCENE = 0xD4;
	public final static int CMD_HC_UPDATE_SCENE_INFO = 0xD6;
	public final static int CMD_HC_UPDATE_SCENE_DELETED = 0xD8;
	public final static int CMD_HC_UPDATE_SCENE_ACTIVATED = 0xD7;
	
	public final static int CMD_APP_CREATE_SCHEDULE = 0xE0;
	public final static int CMD_APP_EDIT_SCHEDULE = 0xE1;
	public final static int CMD_HC_UPDATE_SCHEDULE_INFO = 0xE2;
	public final static int CMD_HC_ACK_SCHEDULE = 0xE3;
	public final static int CMD_APP_TOGGLE_SCHEDULE = 0xE4;
	public final static int CMD_HC_UPDATE_SCHEDULE_STATE = 0xE5;
	public final static int CMD_APP_DEL_SCHEDULE = 0xE6;
	public final static int CMD_HC_UPDATE_SCHEDULE_DELETED = 0xE7;
	
	public final static int CMD_HC_CAUTION_PR = 0xB4;
	
	public final static int VARIABLE_LENGTH = 0;
	public final static int CMD_HC_CONFIG_DEVICE = 0xAA;
	public final static int CMD_HC_CONFIG_SCENE = 0xAB;
	
	public final static int VERSION_CORRECT = 0x01;
	public final static int VERSION_NOT_CORRECT = 0x02;
	
	/**
	 * SO luong loai ban tin config device
	 */
	public  static int numOfTMDE =0; // SO luong loai ban tin config device
	/**
	 * // SO luong loai ban tin config scene
	 */
	public  static int numOfTMSE=0; // SO luong loai ban tin config scene
	
	//byte position
	public final static int cmdIdPos = 0;
	
	//variables
	protected int Cmd_id = CMD_NOTHING;
	protected int length = 0;
	protected int Msg_id = 0;
	
	public NetMessage(){
		
	}
	
	public static final int getCmdId(byte[] input){
		if(input == null)
			return CMD_NOTHING;
		if(input.length<=1)
			return CMD_NOTHING;
		else
			return Byte2Unsigned(input[cmdIdPos]);
	}
	
	public NetMessage(int cmd_id, int Msg_id){
		Cmd_id = cmd_id;
		length = VARIABLE_LENGTH;
		this.Msg_id = Msg_id;
	}
	
	public NetMessage(int cmd_id, int Msg_id, int len){
		Cmd_id = cmd_id;
		length = len;
		this.Msg_id = Msg_id;
	}
	
	public NetMessage(int cmd_id) {
		// TODO Auto-generated constructor stub
		Cmd_id = cmd_id;
	}

	public int getCmd_id(){
		return Cmd_id;
	}
	
	public void setCmd_id(int id){
		Cmd_id = id;
	}
	
	public int getMsg_id(){
		return Msg_id;
	}
	
	public void setMsg_id(int id){
		this.Msg_id = id;
	}
	
	public boolean MsgIDCmp(byte msg){
		if(this.Msg_id == Byte2Unsigned(msg))
			return true;
		else
			return false;
	}
	
	public boolean MsgIDCmp(int msg){
		if(this.Msg_id == (msg))
			return true;
		else
			return false;
	}
	
	//So sanh input co dung la Message khong
	public boolean isCorrectFormat(byte[] input, int length){
		if(input == null || length == 0)
			return false;
		if(this.length != VARIABLE_LENGTH){
			if(length == this.length && input[0] == (byte) this.Cmd_id) return true;
			else return false;
		}
		else{
			if(input[0] == (byte) this.Cmd_id) return true;
			else return false;
		}
	}
	public boolean isCorrectFormatBusy(byte[] input, int length){
		if(input == null || length == 0)
			return false;
		if (this.length != VARIABLE_LENGTH) {
			if (length == this.length && input[0] == (byte) NetMessage.CMD_HC_BUSY) return true;
			else return false;
		}
		else {
			if(input[0] == (byte) NetMessage.CMD_HC_BUSY) return true;
			else return false;
		}
	}
	public final static int Byte2Unsigned(byte a){
		return a & 0xFF;
	}
}


